package com.dbq.share;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.lang.reflect.Field;

/**
 * Created by sunkai on 2017/12/26.
 */

class SharePlatformSelector implements View.OnClickListener {

    private static ShareTarget[] shareTargets = {
            new ShareTarget(SocializeMediaConstant.WEIXIN),
            new ShareTarget(SocializeMediaConstant.WEIXIN_TIMELINE),
            new ShareTarget(SocializeMediaConstant.QQ),
            new ShareTarget(SocializeMediaConstant.QZONE),
            new ShareTarget(SocializeMediaConstant.SINA),
            new ShareTarget(SocializeMediaConstant.FACEBOOK),
            new ShareTarget(SocializeMediaConstant.COPY)
    };
    private final Activity activity;

    protected PopupWindow mShareWindow;
    protected View mAnchorView;
    protected RelativeLayout mContainerView;

    private Animation enterAnimation;

    private Context context;
    private AdapterView.OnItemClickListener onItemClickListener;
    private PopupWindow.OnDismissListener onDismissListener;
    private View pannel;

    public SharePlatformSelector(Activity activity, View mAnchorView, AdapterView.OnItemClickListener onItemClickListener, PopupWindow.OnDismissListener onDismissListener) {
        this.context = mAnchorView.getContext();
        this.mAnchorView = mAnchorView;
        this.activity = activity;
        this.onItemClickListener = onItemClickListener;
        this.onDismissListener = onDismissListener;
    }

    public void show() {
        createShareWindowIfNeed();
        if (!mShareWindow.isShowing()) {
            mShareWindow.showAtLocation(mAnchorView, Gravity.BOTTOM, 0, 0);
        }
        showEnterAnimation();
    }

    private void showEnterAnimation() {
        if (enterAnimation == null){
            enterAnimation = AnimationUtils.loadAnimation(context, R.anim.socialize_shareboard_animation_in);
        }
        pannel.setAnimation(enterAnimation);
        enterAnimation.start();
    }


    private void createShareWindowIfNeed() {
        if (mShareWindow != null) {
            return;
        }

        pannel = createShareGridView(context);
//        RelativeLayout.LayoutParams gridParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        gridParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//        pannel.setLayoutParams(gridParams);
//
//        mContainerView = new RelativeLayout(context);
//        mContainerView.setBackgroundColor(context.getResources().getColor(R.color.dbq_socialize_black_trans));
//        RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        mContainerView.setLayoutParams(containerParams);
//        mContainerView.addView(pannel);
//        mContainerView.setOnClickListener(this);

        mShareWindow = new PopupWindow(pannel, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//         pannel.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        fitPopupWindowOverStatusBar(mShareWindow, true);
        mShareWindow.setOutsideTouchable(true);
        mShareWindow.setAnimationStyle(-1);
        mShareWindow.setBackgroundDrawable(new BitmapDrawable());
        mShareWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (activity != null) {
                    WindowManager.LayoutParams ll = activity.getWindow().getAttributes();
                    ll.alpha = 1f;
                    activity.getWindow().setAttributes(ll);
                }
                onDismissListener.onDismiss();
            }
        });
        if (activity != null) {
            WindowManager.LayoutParams ll = activity.getWindow().getAttributes();
            ll.alpha = 0.6f;
            activity.getWindow().setAttributes(ll);
        }
    }


    public void fitPopupWindowOverStatusBar(PopupWindow mPopupWindow, boolean needFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(true);
                mLayoutInScreen.set(mPopupWindow, needFullScreen);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private View createShareGridView(final Context context) {
        View view = View.inflate(context, R.layout.dbq_socialize_sharing_panel, null);
        GridView grid = (GridView) view.findViewById(R.id.gridview);
        RelativeLayout cancle_btn = (RelativeLayout) view.findViewById(R.id.canclel_btn);
        ListAdapter adapter = new ArrayAdapter<ShareTarget>(context, 0, shareTargets) {
            // no need scroll
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                @SuppressLint("ViewHolder")
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dbq_socialize_shareboard_item, parent, false);
                //view.setBackgroundDrawable(null);
                ImageView image = (ImageView) view.findViewById(R.id.bili_socialize_shareboard_image);
                TextView platform = (TextView) view.findViewById(R.id.bili_socialize_shareboard_pltform_name);

                ShareTarget target = getItem(position);
                image.setImageResource(target.iconId);
                platform.setText(target.titleId);
                return view;
            }
        };
        grid.setNumColumns(-1);
        grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        grid.setColumnWidth(context.getResources().getDimensionPixelSize(R.dimen.dbq_socialize_shareboard_size));
        //grid.setSelector(R.drawable.dbq_socialize_selector_item_background);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(onItemClickListener);
        cancle_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return view;
    }


    @Override
    public void onClick(View v) {
        if (v == mContainerView) {
            dismiss();
        }
    }

    public void dismiss() {
        if (mShareWindow != null) {
            mShareWindow.dismiss();
        }
        cancleWindowAlpha();
    }


    public void cancleWindowAlpha(){
        if(activity!=null){
            WindowManager.LayoutParams ll = activity.getWindow().getAttributes();
            ll.alpha=1f;
            activity.getWindow().setAttributes(ll);
        }

    }
}
